﻿using CPF;
using CPF.Animation;
using CPF.Charts;
using CPF.Controls;
using CPF.Drawing;
using CPF.Shapes;
using CPF.Styling;
using CPF.Svg;
using CPF.Threading;
using CPFUI.IHelper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CPFUI.IControl
{
    [CPF.Design.DesignerLoadStyle("res://$safeprojectname$/Stylesheet1.css")]//用于设计的时候加载样式
    public class ColorExtractorControl : Control
    {
        MouseHook mh;
        public Color SelectColor
        {
            get { return GetValue<Color>(); }
            set { SetValue(value); }
        }
        public string R
        {
            get { return GetValue<string>(); }
            set { SetValue(value); }
        }
        public string G
        {
            get { return GetValue<string>(); }
            set { SetValue(value); }
        }
        public string B
        {
            get { return GetValue<string>(); }
            set { SetValue(value); }
        }
        public string Hex
        {
            get { return GetValue<string>(); }
            set { SetValue(value); }
        }
        public string PositionString
        {
            get { return GetValue<string>(); }
            set { SetValue(value); }
        }
        /// <summary>
        /// 显示设备上下文环境的句柄。
        /// </summary>
        private IntPtr _hdc = IntPtr.Zero;
        bool isStartGetColor = false;
        /// <summary>
        /// 指向窗口的句柄。
        /// </summary>
        private readonly IntPtr _hWnd = IntPtr.Zero;
        DispatcherTimer timer;
        //模板定义
        protected override void InitializeComponent()
        {
            R = "255";
            G = "0";
            B = "0";
            Hex = "#FF00000";
            PositionString = "X:0,Y:0";
            SelectColor = Color.Red;
            Children.Add(new Panel
                    {
                        MarginLeft = 10,
                        MarginTop = 10,
                        Width = 100,
                        Height = 100,
                        Background = SelectColor,
                        Bindings ={
                            {
                                nameof(Panel.Background),nameof(SelectColor),this
                            }
                        },
                        Commands =
                        {
                            {
                                nameof(Panel.MouseDown),(s,e)=>
                                {
                                    IntPtr hcur_click =Win32Helper.LoadCursorFromFile(@"C:\Windows\Cursors\aero_pen_xl.cur");
                                    Win32Helper.SetSystemCursor(hcur_click, Win32Helper.OCR_NORMAL);
                                    Win32Helper.SetSystemCursor(hcur_click, Win32Helper.OCR_IBEAM);
                                    isStartGetColor=true;
                                    _hdc = Win32Helper.GetDC(_hWnd);
                                    timer.Start();
                                }
                            },
                        }
                    });
            Children.Add(new StackPanel
                    {
                        Orientation = Orientation.Vertical,
                        MarginLeft = 115,
                        MarginTop = 10,
                        MarginRight = 10,
                        Children ={
                            new StackPanel{

                                MarginLeft=0,
                                Orientation=Orientation.Horizontal,
                                Children =
                                {
                                     new TextBlock{
                                         Width=60,
                                         TextAlignment=TextAlignment.Right,
                                        Text="红(R):"
                                    },
                                    new TextBox
                                    {
                                        Text=R,
                                        Width=60,
                                        Bindings={
                                            {
                                                nameof(TextBox.Text),nameof(R),this
                                            }
                                        },
                                    },
                                }
                            },
                           new StackPanel{
                               MarginTop=5,
                                MarginLeft=0,
                                Orientation=Orientation.Horizontal,
                                Children =
                                {
                                     new TextBlock{
                                         Width=60,
                                         TextAlignment=TextAlignment.Right,
                                         Text="绿(G):"
                                    },
                                    new TextBox
                                    {
                                        Text=G,
                                        Width=60,
                                        Bindings={
                                            {
                                                nameof(TextBox.Text),nameof(G),this
                                            }
                                        },
                                    },
                                }
                            },
                           new StackPanel{
                               MarginTop=5,
                                MarginLeft=0,
                                Orientation=Orientation.Horizontal,
                                Children =
                                {
                                     new TextBlock{
                                         Width=60,
                                         TextAlignment=TextAlignment.Right,
                                         Text="蓝(B):"
                                    },
                                    new TextBox
                                    {
                                        Text=B,
                                        Width=60,
                                        Bindings={
                                            {
                                                nameof(TextBox.Text),nameof(B),this
                                            }
                                        },
                                    },
                                }
                            },
                           new StackPanel{
                               MarginTop=5,
                                MarginLeft=0,
                                Orientation=Orientation.Horizontal,
                                Children =
                                {
                                     new TextBlock{
                                         Width=60,
                                         TextAlignment=TextAlignment.Right,
                                         Text="十六进制:"
                                    },
                                    new TextBox
                                    {
                                        Text=Hex,
                                        Width=60,
                                        Bindings={
                                            {
                                                nameof(TextBox.Text),nameof(Hex),this
                                            }
                                        },
                                    },
                                }
                            },
                        }
                    });
            Children.Add(new Panel
            {
                MarginLeft = 10,
                MarginTop=115,
                MarginBottom = 10,
                MarginRight = 10,
                Children ={
                            new TextBlock{
                                MarginLeft=0,
                                Width=40,
                                Text="位置：",
                            },
                            new TextBox{
                                Background=null,
                                MarginLeft=40,
                                MarginRight=0,
                                Text=PositionString,
                                Width=185,
                                BorderFill=null,
                                Bindings={
                                    {
                                        nameof(TextBox.Text),nameof(PositionString),this
                                    }
                                }
                            },
                        }
            });
        }

#if !DesignMode //用户代码写到这里，设计器下不执行，防止设计器出错
        protected override void OnInitialized()
        {
            base.OnInitialized();
            isStartGetColor = false;
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(1);
            timer.Tick += Timer_Tick;
            timer.IsEnabled = false;
            mh = new MouseHook();
            mh.SetHook();
            mh.MouseDownEvent += Mh_MouseDownEvent; ;
            mh.MouseUpEvent += Mh_MouseUpEvent; ;
        }

        private void Mh_MouseUpEvent(object sender, CPF.Input.MouseEventArgs e)
        {
            if (!isStartGetColor)
            {
                return;
            }
            isStartGetColor = false;
            timer.Stop();
            Win32Helper.SystemParametersInfo(Win32Helper.SPI_SETCURSORS, Win32Helper.SPIF_SENDWININICHANGE, IntPtr.Zero, Win32Helper.SPIF_SENDWININICHANGE);
            
            Win32Helper.ReleaseDC(_hWnd, _hdc);
        }

        private void Mh_MouseDownEvent(object sender, CPF.Input.MouseEventArgs e)
        {
        }

        private void Timer_Tick(object sender, EventArgs e)
        {

            System.Drawing.Point mp = new System.Drawing.Point();
            Win32Helper.GetCursorPos(out mp);
            uint color = Win32Helper.GetPixel(_hdc, mp.X, mp.Y);
            byte r = GetRValue(color);
            byte g = GetGValue(color);
            byte b = GetBValue(color);
            PositionString = string.Format("X:{0},Y:{1}", mp.X, mp.Y);
            Hex = "#" + r.ToString("X").PadLeft(2, '0') + g.ToString("X").PadLeft(2, '0') +
                               b.ToString("X").PadLeft(2, '0');
            R = r.ToString();
            G = g.ToString();
            B = b.ToString();
            SelectColor = Color.FromRgb(r, g, b);
        }
        /// <summary>
        /// 获取一个RGB颜色值中的红色强度值。
        /// </summary>
        /// <param name="rgb">指定的RGB颜色值。</param>
        /// <returns></returns>
        public byte GetRValue(uint rgb)
        {
            return (byte)rgb;
        }

        /// <summary>
        /// 获取一个RGB颜色值中的绿色强度值。
        /// </summary>
        /// <param name="rgb">指定的RGB颜色值。</param>
        /// <returns></returns>
        public byte GetGValue(uint rgb)
        {
            return (byte)(((ushort)(rgb)) >> 8);
        }

        /// <summary>
        /// 获取一个RGB颜色值中的蓝色强度值。
        /// </summary>
        /// <param name="rgb">指定的RGB颜色值。</param>
        /// <returns></returns>
        public byte GetBValue(uint rgb)
        {
            return (byte)(rgb >> 16);
        }
        //用户代码

#endif
    }
}
